package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.Eim;
import org.yjc.entity.KingRoot;
import org.yjc.entity.Lessee;
import org.yjc.entity.Log;
import org.yjc.entity.SysUser;
import org.yjc.form.EimForm;
import org.yjc.security.UserUtils;
import org.yjc.service.EimService;
import org.yjc.service.LesseeAdminService;
import org.yjc.service.LesseeService;
import org.yjc.service.LogService;

@Controller
@RequestMapping(value="/eim")
public class EimController {
	@Autowired
	private EimService eimservice;
	@Autowired
	private LesseeService lesseeservice; 
	@Autowired
	private LesseeAdminService lesseeadminservice;
	@Autowired
	private UserUtils userutils;
	@Autowired
	private LogService logservice;
	
	@RequestMapping(value="eimmain")
	private String lesseeAll(ModelMap map) {
		List<Lessee> lessees = lesseeservice.findAll(); //获取到全部的租户
		Integer lesseeid = 0;
		if(!lessees.isEmpty()) {
			lesseeid = lessees.get(0).getId();
		}
		map.put("lessees",lessees);
		map.put("lesseeid",lesseeid);
		return "eim/eimmain";
	}
	@RequestMapping(value="/lesseeeim")
	public String alllessee(ModelMap map) {
		SysUser sysuser = new SysUser();
		//获取上下文对象
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username = userDetails.getUsername();
		sysuser = lesseeadminservice.findByUsername(username);
		Integer id = sysuser.getId(); //获取到当前租户管理员的id
		List<Lessee> lessees = lesseeservice.findByLesseeadminId(id); //通过id查询该租户管理员的下级租户
		
		Integer lesseeid = 0;
		if(!lessees.isEmpty()) {
			lesseeid = lessees.get(0).getId();
		} //取每一个租户的id
		map.put("lessees",lessees);
		map.put("lesseeid",lesseeid);
		return "eim/lesseeeim";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(JqgridParam param,Integer lesseeId) {
		Pageable pageable = param.buildPageable();
		Specification spec = new Specification<KingRoot>() {

			@Override
			public Predicate toPredicate(Root<KingRoot> root, CriteriaQuery<?> arg1, CriteriaBuilder cb) {
				Predicate predicate = cb.equal(root.get("lessee").get("id"), lesseeId);
				return predicate;
			}
		};
		Page<Eim> page;
		if(param.getSearch()) {
			spec = spec.and(param.buildSpec());
		}
		page = eimservice.findAll(spec,pageable);
		return JqgridUtils.getPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(String id,EimForm form) {
		String oper = form.getOper();
		if("del".equals(oper)) {
			eimservice.deleteById(Integer.parseInt(id));
			SysUser user = userutils.getUser();	 //获取当前用户
			Log log = new Log();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setName(user.getUsername());
			log.setOperationTime(df.format(new Date())); //获取当前系统时间
			log.setOperatiobName("删除电子发票");
			logservice.save(log);
		}
		return "OK";
	}
}
